package com.zj.syzs.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;

/**
 * Created by ZJ on 2015/6/15 0015.
 */
public class AppUtils {
    //判断是否第一次运行应用
    public static boolean isFirst(Context context) {
        // 从共享参数中读取当前打开程序是否为第一次
        SharedPreferences sp = context.getSharedPreferences("isFirst",
                Context.MODE_PRIVATE);
        int version = sp.getInt("version", 0);
        boolean first = sp.getBoolean("isFirst", true);
        if (first || version != getVersion(context)) {
            sp.edit().putBoolean("isFirst", false).commit();
            sp.edit().putInt("version", getVersion(context)).commit();
        }
        return first;
    }

    public static int getVersion(Context context) {
        int version = 0;
        try {
            PackageInfo info = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
            version = info.versionCode;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }

        return version;
    }
}

